﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenSSL.Crypto;

namespace Sicurezza.Client.Controlli
{
    public partial class CertificateViewControl : UserControl
    {
        public string Version {
            get { return VersionTxt.Text; }
            set { VersionTxt.Text = value; }
        }

        public string Serial {
            get { return SerialTxt.Text; }
            set { SerialTxt.Text = value; }
        }

        public string AlgorithmID {
            get { return AlgorithmIDTxt.Text; }
            set { AlgorithmIDTxt.Text = value; }
        }

        public string NotBefore {
            get { return NotBeforetxt.Text; }
            set { NotBeforetxt.Text = value; }
        }

        public string NotAfter {
            get { return NotAftertxt.Text; }
            set { NotAftertxt.Text = value; }
        }

        public string SubjectO {
            get { return SubjectOtxt.Text; }
            set { SubjectOtxt.Text = value; }
        }

        public string SubjectC {
            get { return SubjectCtxt.Text; }
            set { SubjectCtxt.Text = value; }
        }

        public string SubjectOU {
            get { return SubjectOUtxt.Text; }
            set { SubjectOUtxt.Text = value; }
        }

        public string SubjectCN
        {
            get { return SubjectCNtxt.Text; }
            set { SubjectCNtxt.Text = value; }
        }

        public string SubjectEmail
        {
            get { return SubjectEmailtxt.Text; }
            set { SubjectEmailtxt.Text = value; }
        }

        public string PublicKeyAlgorithm {
            get { return PublicKeyAlgoTxt.Text; }
            set { PublicKeyAlgoTxt.Text = value; }
        }

        public string PublicKey {
            get { return PublicKeyTxt.Text; }
            set { PublicKeyTxt.Text = value; }
        }

        public bool DigitalSignature
        {
            get { return DigitalSignatureBox.Checked; }
            set { DigitalSignatureBox.Checked = value; }
        }

        public bool KeyEncipherment
        {
            get { return KeyEnciphermentBox.Checked; }
            set { KeyEnciphermentBox.Checked = value; }
        }

        public CertificateViewControl()
        {
            InitializeComponent();
        }

        public string IssuerC { 
            get { return IssuerCtxt.Text; }
            set { IssuerCtxt.Text = value; }
        }

        public string IssuerO
        {
            get { return IssuerOtxt.Text; }
            set { IssuerOtxt.Text = value; }
        }

        public string IssuerOU
        {
            get { return IssuerOUtxt.Text; }
            set { IssuerOUtxt.Text = value; }
        }

        public string IssuerCN
        {
            get { return IssuerCNtxt.Text; }
            set { IssuerCNtxt.Text = value; }
        }

        private bool _editMode = false;
        public bool EditMode {
            get { return _editMode; }
            set {
                _editMode = value;
                if (value)
                {
                    IssuerInfoPanel.Visible = false;
                    EnableTextEdit();
                }

                else
                {
                    IssuerInfoPanel.Visible = true;
                    DisableTextEdit();
                }
                
            }
        }

        private bool _readOnlyMode = false;
        public bool ReadOnlyMode
        {
            get { return _readOnlyMode; }
            set
            {
                _readOnlyMode = value;
                if (value)
                {
                    DisableTextEdit();
                }

                else
                {
                    EnableTextEdit();
                }

            }
        }

        private bool _requestMode = false;
        public bool RequestMode
        {
            get { return _requestMode; }
            set
            {
                _requestMode = value;
                if (value)
                {
                    IssuerInfoPanel.Visible = false;
                }

                else
                {
                    IssuerInfoPanel.Visible = true;
                }

            }
        }

        private void DisableTextEdit()
        {
            foreach (Control c in IssuerInfoPanel.Controls)
            {
                TextBox t = c as TextBox;
                if (t != null) t.ReadOnly = true;
                
            }

            foreach (Control c in SubjectInfoPanel.Controls)
            {
                TextBox t = c as TextBox;
                if (t != null) t.ReadOnly = true;
                else
                {
                    CheckBox b = c as CheckBox;
                    if (b != null) b.Enabled = false;
                }
            }

        }

        private void EnableTextEdit()
        {
            foreach (Control c in SubjectInfoPanel.Controls)
            {
                TextBox t = c as TextBox;
                if (t != null) t.ReadOnly = false;
                else
                {
                    CheckBox b = c as CheckBox;
                    if (b != null) b.Enabled = true;
                }
            }

            PublicKeyAlgoTxt.ReadOnly = true;
            PublicKeyTxt.ReadOnly = true;
        }

        private void CertificateForm_Load(object sender, EventArgs e)
        {

        }

        public void Reset()
        {
            foreach (Control c in IssuerInfoPanel.Controls)
            {
                TextBox t = c as TextBox;
                if (t != null) t.Text = "";
            }

            foreach (Control c in SubjectInfoPanel.Controls)
            {
                TextBox t = c as TextBox;
                if (t != null) t.Text = "";
                else
                {
                    CheckBox b = c as CheckBox;
                    if (b != null) b.Checked = false;
                }
            }
        }
    }
}
